package com.ruoyi.web.controller.announcement.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.entity.DO.ShopOrderAnnounceDO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.announcement.mapper.IShopOrderAnnounceMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/back/shopOrderAnnounce")
public class BackShopOrderAnnounceController extends BaseController {
    private String prefix = "order/announce";

    @Resource
    private IShopOrderAnnounceMapper shopOrderAnnounceMapper;

    @RequiresPermissions("back:shopOrderAnnounce:list")
    @GetMapping("/list")
    public String list(ModelMap mmap) {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        List<Long> idList = shopOrderAnnounceMapper.getLastestIdList5( ShiroUtils.getShopId());
        mmap.put("playedIds", StringUtils.join(idList,","));
        return prefix + "/list";
    }

    @RequiresPermissions("back:shopOrderAnnounce:bigList")
    @GetMapping("/bigList")
    public String bigList(ModelMap mmap) {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        List<Long> idList = shopOrderAnnounceMapper.getLastestIdList5( ShiroUtils.getShopId());
        mmap.put("playedIds", StringUtils.join(idList,","));
        return prefix + "/bigList";
    }

    /**
     * 获取语音播报队列
     */
    @RequiresPermissions("back:shopOrderAnnounce:list")
    @GetMapping("/getLastestList/{lastId}")
    @ResponseBody
    public AjaxResult getLastestList(@PathVariable("lastId") Long lastId) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        List<ShopOrderAnnounceDO> list= new ArrayList<>();
        if( 0 == lastId){
            ShopOrderAnnounceDO one = shopOrderAnnounceMapper.getLastestOne(ShiroUtils.getShopId(),lastId);
            if(null != one){
                list.add(one);
            }
        }else{
            list = shopOrderAnnounceMapper.getLastestList(ShiroUtils.getShopId(),lastId);
        }
        return AjaxResult.success(list);
    }
}
